package com.ckd.flyingtrip.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.ckd.flyingtrip.R;
import com.ckd.flyingtrip.constants.Constants;
import com.ckd.flyingtrip.dialog.PayWayDialog;
import com.ckd.flyingtrip.javabean.UserInfoBean;
import com.ckd.flyingtrip.utils.BcUtils;
import com.ckd.flyingtrip.utils.DataCheck;
import com.ckd.flyingtrip.utils.Tools;
import com.ckd.flyingtrip.utils.VolleyErrorUtils;
import com.ckd.flyingtrip.widge.SingleVolleyRequestQueue;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MyZhanghuActivity extends BaseActivity {
    @BindView(R.id.mycard)
    TextView mycard;
    @BindView(R.id.txt_yajin)
    TextView txtYajin;
    @BindView(R.id.btn_yajin)
    Button btnYajin;
    @BindView(R.id.mycarda)
    TextView mycarda;
    @BindView(R.id.txt_zhujin)
    TextView txtZhujin;
    @BindView(R.id.btn_zujin)
    Button btnZujin;
    @BindView(R.id.toolber_color_title)
    TextView title;
    private PayWayDialog.Builder pb;
    private String id;

    //押金
    private String userDeposit = "0";
    private String userCarDeposit = "0";
    //租金
    private String userRent = "0";
    private String userCarRent = "0";
    //电动车码
    private String bindingElectricBicycle = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_zhanghu);
        ButterKnife.bind(this);
        title.setText("我的账户");
        id = getIntent().getExtras().getString("id");
//        volleyUserInfo();

        if (getIntent().getStringExtra("deposit") == null || getIntent().getStringExtra("rent") == null)
            return;
        mycard.setText("我的押金（" + getIntent().getStringExtra("deposit") + "）");
        mycarda.setText("我的租金（" + getIntent().getStringExtra("rent") + "）");
    }

    @OnClick({R.id.btn_yajin, R.id.btn_zujin, R.id.toolber_color_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.toolber_color_back:
                finish();
                break;
            case R.id.btn_yajin:
                if (BcUtils.getUserInfo().getUserSignState().equals("") || BcUtils.getUserInfo().getUserSignState().equals("0")) {
                    startActivity(new Intent(this, MyZhanghuPactActivity.class));
                    return;
                }

                if (!bindingElectricBicycle.equals("")) {
                    if (!bindingElectricBicycle.equals(getIntent().getStringExtra("code"))) {
                        toast("您已经有自己的车，不能在租用第二辆了！");
                        return;
                    }
                }

                if (!userCarDeposit.equals("0") && !userCarDeposit.equals("")) {
                    toast("已缴纳押金！");
                    return;
                }

                pb = new PayWayDialog.Builder(MyZhanghuActivity.this,
                        new PayWayDialog.Builder.PriorityListener() {
                            @Override
                            public void refreshPriorityUI(String f) {
                                volleyQueryFei(id, f, "1");
                                pb.create().dismiss();
                            }
                        });
                pb.create().show();
                break;
            case R.id.btn_zujin:
                if (BcUtils.getSignState().equals("") || BcUtils.getSignState().equals("0")) {
                    startActivity(new Intent(this, MyZhanghuPactActivity.class));
                    return;
                }

                if (!bindingElectricBicycle.equals("")) {
                    if (!bindingElectricBicycle.equals(getIntent().getStringExtra("code"))) {
                        toast("您已经有自己的车，不能在租用第二辆了！");
                        return;
                    }
                }

                if (userCarDeposit.equals("0") || userCarDeposit.equals("")) {
                    toast("请先缴纳押金！");
                    return;
                }

                if (userCarRent.equals("12")) {
                    toast("已缴纳租金！");
                    return;
                }

                pb = new PayWayDialog.Builder(MyZhanghuActivity.this,
                        new PayWayDialog.Builder.PriorityListener() {
                            @Override
                            public void refreshPriorityUI(String f) {

                                volleyQueryFei(id, f, "2");

                                pb.create().dismiss();
                            }
                        });
                pb.create().show();
                break;
        }
    }


    /*
     * 查询费用 准备掉起支付
     * */
    public void volleyQueryFei(String id, String type, String yjzj) {
        if (type.equals("weixin")) {
//            Weixin weixin = new Weixin(GoumaiActivity.this, "");
//            weixin.volleyDINGDAN(id);
            dismisProgressDialog();
        } else if (type.equals("alipay")) {
            createLoadingDialog(this, "正在支付中...", true);
            Alipay alipay = new Alipay(MyZhanghuActivity.this, "");
            alipay.volleyQueryzuche(id, yjzj);
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        volleyUserInfo();
    }

//    @Override
//    protected void onRestart() {
//        super.onRestart();
//        volleyUserInfo();
//    }

    public void volleyUserInfo() {
        final Gson mGson = new Gson();
        String url = Constants.QINGQIUGERENXINXI;
        StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String s) {
                dismisProgressDialog();
                Log.i("用户信息", s);
                //s为请求返回的字符串数据
                try {
                    JSONObject json = new JSONObject(s);
                    String code = json.getString("code");
                    String msg = json.getString("msg");
                    if (code.equals("1")) {
                        JSONObject dataInfo = json.getJSONObject("data");
                        String data = json.getString("data");
                        UserInfoBean userInfoBean = mGson.fromJson(data, UserInfoBean.class);
                        BcUtils.setUserInfo(userInfoBean);
                        BcUtils.setuserDeposit(userInfoBean.getUserDeposit());
                        BcUtils.setuserRent(userInfoBean.getUserRent());
                        userDeposit = userInfoBean.getUserDeposit();
                        userCarDeposit = userInfoBean.getUserCarDeposit();
                        userRent = userInfoBean.getUserRent();
                        userCarRent = userInfoBean.getUserCarRent();
                        bindingElectricBicycle = userInfoBean.getBindingElectricBicycle();

                        if (userCarDeposit.equals("0")) {
                            txtYajin.setText("未缴纳押金");
                        } else {
                            txtYajin.setText("已缴纳押金");
                        }

                        if (userCarRent.equals("0")) {
                            txtZhujin.setText("未缴纳租金");
                        } else {
                            txtZhujin.setText("已缴纳租金");
                        }

                        if (!bindingElectricBicycle.equals(""))
                            if (!bindingElectricBicycle.equals(getIntent().getStringExtra("code"))) {
                                txtYajin.setText("该车未缴纳押金");
                                txtZhujin.setText("该车未缴纳租金");
                            }

                    } else {
                        Log.i("站好", "账号");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    dismisProgressDialog();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                dismisProgressDialog();
                VolleyErrorUtils.checkError(volleyError);
            }
        }) {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {//创建请求参数

                HashMap<String, String> map = new HashMap<String, String>();
                JSONObject json_url = new JSONObject();
                try {
                    json_url.put("userId", BcUtils.getUid());
                    json_url.put("userPhone", BcUtils.getUserInfo().getUserPhone());
                    json_url.put("token", BcUtils.getToken());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                map.put("inputParameter", DataCheck.signCheck(json_url.toString()));
                return map;
            }
        };
        SingleVolleyRequestQueue.getInstance(this).addToRequestQueue(request);
    }

}
